package com.swcw.platform.constants;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author openJDK
 */
public class MExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// 根据不同错误转向不同页面
		Logger log = Logger.getLogger(MExceptionHandler.class);
		log.info("系统开始异常啦。。。。。===============>" + ex.getMessage() + "===================" + ex.toString());
		if (ex instanceof NullPointerException) {
			log.info("空指针异常" + ex.getLocalizedMessage());
			ex.printStackTrace();
		} else {
			if (ex.getMessage().contains("OutOfMemoryError")) {
				log.info("JVM内存异常");
				List<String> mobiles = new ArrayList<>();
				mobiles.add("18951603280");
			} else {
				log.info("其他异常" + ex.getLocalizedMessage());
				ex.printStackTrace();
			}
		}
		ModelAndView modelAndView = new ModelAndView("500.html");
		modelAndView.addObject("msg", ex.getMessage());
		return modelAndView;
	}
}